package com.lsk.finance;

import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class FinanceService {
    /**
     * 财务统计service
     * @return
     */
    public Map financeStatisticsService(String startDate,String endDate,String activityId){

        Map resultMap=new HashMap();
        /*订单统计*/
        Record orderStatistics=statisticsOrder(startDate,endDate,activityId);
        /*分销返利统计*/
        Record rebateStatistics=statisticsRebate(startDate,endDate,activityId);
        /*拼团退款统计*/
        Record refundStatistics=statisticsRefund(startDate,endDate,activityId);
        /*推广返利统计*/
        Record shareStatistics=statisticsGeneralize(startDate,endDate,activityId);
        /*提现统计*/
        Record withdrawStatistics=statisticsWithdraw(startDate,endDate,activityId);
        /*手续费*/
        double commission=new BigDecimal(orderStatistics.getDouble("money"))
                .multiply(new BigDecimal(0.006)).setScale(2,BigDecimal.ROUND_HALF_UP)
                .doubleValue();
        /*系统结余*/
        double balance=new BigDecimal(orderStatistics.getDouble("money"))
                .subtract(new BigDecimal(rebateStatistics.getDouble("money")))
                .subtract(new BigDecimal(refundStatistics.getDouble("money")))
                .subtract(new BigDecimal(shareStatistics.getDouble("money")))
                .subtract(new BigDecimal(withdrawStatistics.getDouble("money")))
                .subtract(new BigDecimal(commission)).setScale(2,BigDecimal.ROUND_HALF_UP)
                .doubleValue();
        resultMap.put("balance",balance);
        resultMap.put("commission",commission);
        resultMap.put("orderStatistics",orderStatistics);
        resultMap.put("rebateStatistics",rebateStatistics);
        resultMap.put("refundStatistics",refundStatistics);
        resultMap.put("shareStatistics",shareStatistics);
        resultMap.put("withdrawStatistics",withdrawStatistics);
        return resultMap;
    }
    /**
     * 订单统计
     */
    public Record statisticsOrder(String startDate,String endDate,String activityId){
        Record orderRecord=new Record();
        String sql=" SELECT\n" +
                "\tCOUNT(*) AS number,\n" +
                "\t  IFNULL(SUM(money),0.00)/100   AS money\n" +
                "FROM\n" +
                "\tgroup_order  where 1=1 " ;
        if ("".equals(startDate)==false){//开始不为空
            sql+=" and  DATE_FORMAT(createTime,'%Y-%m-%d')>='"+startDate+"' ";
        }
        if ("".equals(endDate)==false){//结束不为空
            sql+=" and  DATE_FORMAT(createTime,'%Y-%m-%d')<='"+endDate+"' ";
        }
        if ("".equals(activityId)==false && "null".equals(activityId)==false && activityId!=null){//活动ID不为空
            sql+=" and  activityId='"+activityId+"' ";
        }
        orderRecord= Db.findFirst(sql);
        if (orderRecord==null){
            orderRecord.set("number",0).set("money",0.00);
        }
        return orderRecord;
    }
    /**
     * 分销返利
     */
    public Record statisticsRebate(String startDate,String endDate,String activityId){

        Record orderRecord=new Record();
        String sql=" SELECT\n" +
                "\tCOUNT(*) AS number,\n" +
                "\tIFNULL(SUM(money),0.00)/100 AS money\n" +
                "FROM\n" +
                "\tgroup_rebate_log  where 1=1 " ;
        if ("".equals(startDate)==false){//开始不为空
            sql+=" and  DATE_FORMAT(createTime,'%Y-%m-%d')>='"+startDate+"' ";
        }
        if ("".equals(endDate)==false){//结束不为空
            sql+=" and  DATE_FORMAT(createTime,'%Y-%m-%d')<='"+endDate+"' ";
        }
        if ("".equals(activityId)==false && "null".equals(activityId)==false && activityId!=null){//活动ID不为空
            sql+=" and  activityId='"+activityId+"' ";
        }
        orderRecord= Db.findFirst(sql);
        if (orderRecord==null){
            orderRecord.set("number",0).set("money",0.00);
        }
        return orderRecord;

    }
    /**
     * 退款统计
     */
    public Record statisticsRefund(String startDate,String endDate,String activityId){

        Record orderRecord=new Record();
        String sql=" SELECT\n" +
                "\tCOUNT(*) AS number,\n" +
                "\tIFNULL(SUM(money),0.00)/100 AS money\n" +
                "FROM\n" +
                "\tgroup_refund_log  where 1=1 " ;
        if ("".equals(startDate)==false){//开始不为空
            sql+=" and  DATE_FORMAT(createTime,'%Y-%m-%d')>='"+startDate+"' ";
        }
        if ("".equals(endDate)==false){//结束不为空
            sql+=" and  DATE_FORMAT(createTime,'%Y-%m-%d')<='"+endDate+"' ";
        }
        if ("".equals(activityId)==false && "null".equals(activityId)==false && activityId!=null){//活动ID不为空
            sql+=" and  activityId='"+activityId+"' ";
        }
        orderRecord= Db.findFirst(sql);
        if (orderRecord==null){
            orderRecord.set("number",0).set("money",0.00);
        }
        return orderRecord;

    }

    /**
     * 推广返利统计
     */
    public Record statisticsGeneralize(String startDate,String endDate,String activityId){

        Record orderRecord=new Record();
        String sql=" SELECT\n" +
                "\tCOUNT(*) AS number,\n" +
                "\tIFNULL(SUM(money),0.00)/100 AS money\n" +
                "FROM\n" +
                "\tgroup_share_rebate_log  where 1=1 " ;
        if ("".equals(startDate)==false){//开始不为空
            sql+=" and  DATE_FORMAT(createTime,'%Y-%m-%d')>='"+startDate+"' ";
        }
        if ("".equals(endDate)==false){//开始不为空
            sql+=" and  DATE_FORMAT(createTime,'%Y-%m-%d')<='"+endDate+"' ";
        }
        if ("".equals(activityId)==false && "null".equals(activityId)==false && activityId!=null){//活动ID不为空
            sql+=" and  activityId='"+activityId+"' ";
        }
        orderRecord= Db.findFirst(sql);
        if (orderRecord==null){
            orderRecord.set("number",0).set("money",0.00);
        }
        return orderRecord;

    }
    /**
     *所有的活动
     */
    public List<Record> getAllActivity(){
        String sql="SELECT *from group_activity ";
        List<Record> list=Db.find(sql);
        return list;
    }
    /**
     * 提现
     */
    public Record statisticsWithdraw(String startDate,String endDate,String activityId){
        Record orderRecord=new Record();
        String sql=" SELECT\n" +
                "\tCOUNT(*) AS number,\n" +
                "\tIFNULL(SUM(money),0.00)/100 AS money\n" +
                "FROM\n" +
                "\tgroup_withdraw_log  where 1=1 " ;
        if ("".equals(startDate)==false){//开始不为空
            sql+=" and  DATE_FORMAT(createTime,'%Y-%m-%d')>='"+startDate+"' ";
        }
        if ("".equals(endDate)==false){//开始不为空
            sql+=" and  DATE_FORMAT(createTime,'%Y-%m-%d')<='"+endDate+"' ";
        }
        if ("".equals(activityId)==false && "null".equals(activityId)==false && activityId!=null){//活动ID不为空
            sql+=" and  activityId='"+activityId+"' ";
        }
        orderRecord= Db.findFirst(sql);
        if (orderRecord==null){
            orderRecord.set("number",0).set("money",0.00);
        }
        return orderRecord;
    }

}
